
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Anphunl
 */
public class XSprite {
    Sprite sprite=null;
    int frameW,frameH;
    byte anim[][]={{}};
    
    public XSprite(String filename, int fW, int fH, byte[][] anim) {
        try{

            frameH=fH;
            frameW=fW;
            this.anim=anim;
            sprite=new Sprite(Image.createImage(filename),frameW,frameH);
            sprite.setVisible(true);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }

    public int animCount(){
        return anim.length;
    }

    public int animLength(int animId){
        if(animId>=animCount())
            return 0;
        return anim[animId].length;
    }

    public void paintFrame(Graphics g,int frameId,int x,int y){
        sprite.setPosition(x, y);
        sprite.setFrame(frameId);
        sprite.paint(g);
    }

    public void paintAnim(Graphics g,int animId,long curFrame,int x,int y){
        paintFrame(g, anim[animId][(int)(curFrame%animLength(animId))], x, y);
    }
}
